#!/usr/bin/env python

import pygame
from pygame.locals import *

import sys

import pgu

from pgu import gui

class DrawingArea(gui.Widget):
  def __init__(self, **props):
    gui.Widget.__init__(self, **props)
    
    self.w, self.h = 200, 100
    self.surface = pygame.Surface((self.w, self.h), SRCALPHA, 32)
    self.surface.fill((255, 255, 255, 127))
    self.update_rect = pygame.Rect(0, 0, 1, 1)
    
  def paint(self, s):
    #print 'paint'
    self.update_rect = s.blit(self.surface, (0, 0))
    #print self.update_rect
  
  def update(self, s):
    print 'update'
    #return self.paint(s)
    #self.paint(s)
    #return [pygame.Rect(0, 0, self.w, self.h)]
    return None
    
  def event(self, e):
    #print e
    pass
  
  def resize(self, width=None, height=None):
    return self.w, self.h

class CoolTable(gui.Table):
  def __init__(self, **properties):
    gui.Table.__init__(self, **properties)
    
    text_color = (255, 255, 255)
    
    def onClick(value):
      self.focus()
      print value
    
    self.tr()
    
    l = gui.Label("Phil's Pygame GUI",color=text_color, 
              font=pygame.font.Font('vlgothic.ttf', 18))
    l.connect(gui.CLICK, onClick, 'Hi')
    
    self.td(l, align=-1)
    self.rows = 1
  
  def event(self, e):
    gui.Table.event(self, e)
    self.rows += 1
    if e.type == MOUSEBUTTONDOWN:
      self.add(gui.Label('Yes'), col=0, row=self.rows)
  


if __name__ == "__main__":
  pygame.init()
  
  screen = pygame.display.set_mode((640,480),HWSURFACE)
  
  
  app = gui.App()
  
  container = gui.Container(align=-1,valign=-1)
  container.add(CoolTable(font=pygame.font.Font('vlgothic.ttf', 10)), 100, 0)
  
  button = gui.Button('Click me')

  dialog = gui.Dialog(gui.Label('Hi'), gui.Table())

  #container.add(button, 100, 50)
  container.add(DrawingArea(), 300, 300)
  #container.add(dialog, 100, 100)
  
  app.init(container)
  
  #dialog.open(100, 100)
  
  while True:
    screen.fill((200, 100, 0))
    for e in pygame.event.get():
      if e.type is QUIT:
        pygame.quit()
        sys.exit()
      app.event(e)
    app.paint(screen)
    pygame.display.flip()
    pygame.time.wait(10)
